package cn.com.dhc.danlu.activity.category;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.ACache;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.hd.mall.web.common.Tuple;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.DailyUsedCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.DrinksCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.ElectricCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.GrainCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.HardwareCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.NonStapleCategoryItem;
import cn.com.hd.mall.web.webservices.entity.response.merch.MerchCategoryListResponse.OthersCategoryItem;

/**
 * 饮料、粮油、副食、其他的分类二级选项页面
 */
public class GoodsClassifyActivity extends Activity {
    private ClassifyRecyclerAdapter mRecyclerAdapter;
    private List classifyList;  //分类的数据
    private List classifyNameList; //分类名的数据
    private List classifyCodeList; //分类码的数据
    private ACache aCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_classify);

        initView();
    }

    public void classifyClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel_classify:  //取消
                finish();
                break;

            case R.id.btn_sure_classify:   //确定
                Intent backData = new Intent();
                //如果点击选择了二级分类，则返回所选二级分类名及分类码，并放入缓存
                if (mRecyclerAdapter.getCurrentSelectIndex() != -1) {
                    String chooseClassify = (String) classifyNameList.get(mRecyclerAdapter.getCurrentSelectIndex());
                    String chooseClassifyCode = (String) classifyCodeList.get(mRecyclerAdapter.getCurrentSelectIndex());
                    aCache.remove(CommonConst.ACacheComstants.PROPERTY);  // 清空缓存
                    CommonUtils.cachingPropertyCode(getApplicationContext(), "分类", chooseClassifyCode, chooseClassify);
                    backData.putExtra("chooseClassifyCode", chooseClassifyCode);   //属性码
                    backData.putExtra("chooseClassify", chooseClassify);   //属性名
                } else {
                    //获取缓存中的二级分类码和分类名
                    CommonUtils.CachingArrayList tuple = (CommonUtils.CachingArrayList) ACache.get(this).getAsObject(CommonConst.ACacheComstants.PROPERTY);
                    if (tuple != null) {
                        backData.putExtra("chooseClassifyCode", tuple.get(0).getSecondValue());   //属性码
                        backData.putExtra("chooseClassify", tuple.get(0).getThirdValue());   //属性名
                    }
                }
                setResult(RESULT_OK, backData);
                classifyNameList.clear();
                finish();
                break;

            default:
                break;
        }
    }

    /**
     * 设置页面宽高及显示位置
     */
    private void initView() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.height = (int) (displayMetrics.heightPixels - displayMetrics.density * 53); //页面高
        layoutParams.width = (int) (displayMetrics.widthPixels * 0.8); //页面宽
        getWindow().setAttributes(layoutParams);
        getWindow().setGravity(Gravity.RIGHT | Gravity.TOP);  //设置页面显示位置
        aCache = ACache.get(getApplicationContext());    //缓存对象
        // 初始化意图对象
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        classifyNameList = new ArrayList();
        classifyCodeList = new ArrayList();
        List dataList = getDataList(bundle);  //得到分类名列表
        String lastChoose = bundle.getString("lastChoose");
        int index = classifyNameList.indexOf(lastChoose);
        mRecyclerAdapter = new ClassifyRecyclerAdapter(dataList, index, GoodsClassifyActivity.this);
        GridLayoutManager layoutManager = new GridLayoutManager(GoodsClassifyActivity.this, 3);  //设置为GridView样式

        final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(layoutManager); //设置布局管理器
        mRecyclerView.addItemDecoration(new DividerItemDecoration(22, 20)); //设置item间距
        mRecyclerView.setAdapter(mRecyclerAdapter);  //设置Adapter
    }

    /**
     * 获取分类名数据列表
     *
     * @param bundle
     *         上一页面传递过来
     *
     * @return 分类名数据列表
     */
    private List getDataList(Bundle bundle) {
        String tabName = bundle.getString("分类");  //一级分类名
        switch (tabName) {
            case "饮料":   //获取饮料分类数据
                DrinksCategoryItem mDrinksClassifyItem = (DrinksCategoryItem) bundle.getSerializable(CommonConst.IntentKey.DRINKS_CLASSIFY_CONSTANT);
                classifyList = mDrinksClassifyItem.getCategoryTupleList();
                break;

            case "粮油":   //获取粮油分类数据
                GrainCategoryItem mGrainClassifyItem = (GrainCategoryItem) bundle.getSerializable(CommonConst.IntentKey.GRAIN_CLASSIFY_CONSTANT);
                classifyList = mGrainClassifyItem.getCategoryTupleList();
                break;

            case "副食":   //获取副食分类数据
                NonStapleCategoryItem mNonStapleClassifyItem = (NonStapleCategoryItem) bundle.getSerializable(CommonConst.IntentKey.NON_STAPLE_CLASSIFY_CONSTANT);
                classifyList = mNonStapleClassifyItem.getCategoryTupleList();
                break;

            case "日化用品":   //获取日化用品分类数据
                DailyUsedCategoryItem mDailyUsedClassifyItem = (DailyUsedCategoryItem) bundle.getSerializable(CommonConst.IntentKey.DAILY_USED_CLASSIFY_CONSTANT);
                classifyList = mDailyUsedClassifyItem.getCategoryTupleList();
                break;

            case "五金":   //获取五金分类数据
                HardwareCategoryItem mHardwareClassifyItem = (HardwareCategoryItem) bundle.getSerializable(CommonConst.IntentKey.HARDWARE_CLASSIFY_CONSTANT);
                classifyList = mHardwareClassifyItem.getCategoryTupleList();
                break;

            case "家电":   //获取家电分类数据
                ElectricCategoryItem mElectricClassifyItem = (ElectricCategoryItem) bundle.getSerializable(CommonConst.IntentKey.ELECTRIC_CLASSIFY_CONSTANT);
                classifyList = mElectricClassifyItem.getCategoryTupleList();
                break;

            case "其它":   //获取其它分类数据
                OthersCategoryItem mOthersClassifyItem = (OthersCategoryItem) bundle.getSerializable(CommonConst.IntentKey.OTHERS_CLASSIFY_CONSTANT);
                classifyList = mOthersClassifyItem.getCategoryTupleList();
                break;

            default:
                break;
        }
        if (classifyList != null) {
            for (int i = 0; i < classifyList.size(); i++) {
                Tuple tuple = (Tuple) classifyList.get(i);
                String classifyName = (String) tuple.getSecondValue();
                String classifyCode = (String) tuple.getFirstValue();
                classifyCodeList.add(classifyCode);
                classifyNameList.add(classifyName);
            }
        }

        return classifyNameList;
    }
}
